#include "nstar_adt_httputil.h"
#include "nstar_adt_http_page.h"
#include "adp_env.h"
#include "ebm_env.h"



#define JS_P1_E1 		"volume"
#define JS_P1_E2 		"amplifier"
#define JS_P1_E3 		"hearttp"
#define JS_P1_E4 	    "backtp"
#define JS_P1_E5 	    "printctl"
#define JS_P1_E6 	    "fmfre"
#define JS_P1_E7 	    "fmtp"


#define C_PAGE_NAME HTML_PAGE3_NAME

#define C_PAGE_BODY "<body>"\
"<ul>"\
"<li><a class='home'>STAR-斯达鑫辉</a></li>"\
"<li><a href='"HTML_PAGE1_NAME".html'>参数状态</a></li>"\
"<li><a href='"HTML_PAGE2_NAME".html'>网络设置</a></li>"\
"<li><a href='"HTML_PAGE3_NAME".html' class='active'>音频设置</a></li>"\
"<li><a href='"HTML_PAGE4_NAME".html'>信息日志</a></li>"\
"<li><a href='"HTML_PAGE5_NAME".html'>固件升级</a></li>"\
"</ul>"\
"<h2> chengdu sida web management interface </h2>"\
"<h3> 音频设置</h3>"\
"<div style='background:snow; display:block; padding:10px 10%;'>"\
"<form id='frmSetting' method='POST' action='"C_PAGE_NAME".cgi'>"\
"<p><label>音量:</label><input type='text' size='16' id='"JS_P1_E1"' name='"JS_P1_E1"'/></p>"\
"<p><label>功放开关:</label><input type='text' size='16' id='"JS_P1_E2"' name='"JS_P1_E2"'/><b>提示：1开启功放，0关闭</b></p>"\
"<p><label>心跳周期:</label><input type='text' size='16' id='"JS_P1_E3"' name='"JS_P1_E3"'/></p>"\
"<p><label>回传周期:</label><input type='text' size='16' id='"JS_P1_E4"' name='"JS_P1_E4"'/></p>"\
"<p><label>调频频率:</label><input type='text' size='16' id='"JS_P1_E6"' name='"JS_P1_E6"'/></p>"\
"<p><label>调频指令周期:</label><input type='text' size='16' id='"JS_P1_E7"' name='"JS_P1_E7"'/></p>"\
"<p><label>打印控制:</label><input type='text' size='16' id='"JS_P1_E5"' name='"JS_P1_E5"'/><b>1接收，2指令，3音频，4调频</b></p>"\
"<p><p><input type='button' value='保存' onclick ='commit();'  id='rcorners1'/>"\
"<input type='button' onclick ='savereboot();' value='重启终端' id='rcorners1' style='background: #ff6300'/></p>"\
"</form>"\
"</div>"\
"</body>"\
"</html>"


static unsigned int get_consle(void)
{	
	unsigned int ret=0;
	if(console_lv_get(1))
		ret+=1;
	if(console_lv_get(2))
		ret+=10;
	if(console_lv_get(3))
		ret+=100;
	if(console_lv_get(4))
		ret+=1000;
	return ret;
}


static char* get_fm_fre(void)
{	
	static char buf[12];
	unsigned int fre= adpEnv_getfmfq();
	snprintf(buf, 12, "%02x.%02x", fre>>8, fre&0xff);
	return buf;
}


static void _add_htm_element(unsigned char mode)
{
	if(mode == URI_REPOS_HTML){
		REQUST_JSCRIPT_ELEMENT(JS_P1_E1);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E2);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E3);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E4);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E5);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E6);
		REQUST_JSCRIPT_ELEMENT(JS_P1_E7);
	}
	else if(mode == URI_REPOS_JSON){
		unsigned char vol = p_readcfg->vol;
		unsigned char amp_on = p_readcfg->flgAmplifier;
		http_sprintf("'%s':'%02d',",JS_P1_E1, vol);
		http_sprintf("'%s':'%d',",JS_P1_E2, amp_on);
		http_sprintf("'%s':'%d',",JS_P1_E3, adpEnv_get_heart_tp());
		http_sprintf("'%s':'%d',",JS_P1_E4, adpEnv_get_back_tp());
		http_sprintf("'%s':'%d',",JS_P1_E5, get_consle());
		http_sprintf("'%s':'%s',",JS_P1_E6, get_fm_fre());
		http_sprintf("'%s':'%d',",JS_P1_E7, adpEnv_fm_tp_get());
	}
}

static unsigned char web_set_vol(const char* val_str)
{
	unsigned char val= atoi(val_str);
	
	return adpEnv_set_vol(val);
}

static unsigned char web_set_amp_on(const char* val_str)
{
	unsigned char val= atoi(val_str);
	return adpEnv_set_trumpet(val);
}


static unsigned char web_set_back_tp(const char* val_str)
{
	unsigned short val= atoi(val_str);
	return adpEnv_set_back_tp(val);
}

static unsigned char web_set_heart_tp(const char* val_str)
{
	unsigned short val= atoi(val_str);
	return adpEnv_set_heart_tp(val);
}

static unsigned char web_set_fm_tp(const char* val_str)
{
	unsigned short val= atoi(val_str);
	return adpEnv_set_fm_tp(val);
}



static unsigned char web_set_fmfre(const char* val_str)
{
	unsigned char ret=0;
	char *p_dot= strstr(val_str, ".");
	unsigned int fre=0, val1, val2;
	if(p_dot == NULL){
		val2= 0;
	}
	else {
		val2= atoi(p_dot+1);
	}
	val1= atoi(val_str);
	printf("fr= %d.%d\n", val1, val2);
	if(val1 <= 108 && val1 >= 87 && val2 < 100){
		printf("fr= %d.%d\n", val1, val2);
		if(val1 > 100)
			fre|= 0x10000;
		val1%=100;
		fre|= ((val1 /10) << 12);
		val1%=10;
		fre|= (val1 << 8);
		
		if(val2 < 10){
			fre|= (val2 << 4);
		}
		else{
			fre|= (val2/10 << 4);
			val2%=10;
			fre|= val2;
		}
		printf("fm fre%x\n", fre);
		ret= adpEnv_setfmfq(fre);
		if(ret == 1){
			//zxc_env_set_fre(fre);
		}
	}	
	return ret;
}




static unsigned char web_set_consle(const char* val_str)
{
	unsigned short val= atoi(val_str);
	unsigned ch1,ch2,ch3,ch4;
	ch4= val/1000 == 1 ? 1: 0;
	ch3= (val/100)%10 == 1 ? 1: 0;
	ch2= (val/10)%10 == 1 ? 1: 0;
	ch1= (val/1)%10 == 1 ? 1: 0;
	
	if(ch1)
		console_lv_set(1);
	else
		console_lv_clear(1);
		
	if(ch2)
		console_lv_set(2);
	else
		console_lv_clear(2);

	if(ch3)
		console_lv_set(3);
	else
		console_lv_clear(3);

	if(ch4)
		console_lv_set(4);
	else
		console_lv_clear(4);
	return 1;
}

void parm3_pos_htm(unsigned char mode)
{
	if(mode == URI_REPOS_HTML)
		http_page_htm(C_PAGE_NAME, C_PAGE_BODY, _add_htm_element);
	else if(mode == URI_REPOS_JSON)
		http_page_json(C_PAGE_NAME, _add_htm_element);
}

void parm3_rpos_cgi(char *url)
{ 
	char *p_content; 
	p_content = (char*)my_get_param_url(url);
	if(NULL == p_content){
		return;
	}

	http_handle_parm(p_content, JS_P1_E1, (void*)web_set_vol);
	http_handle_parm(p_content, JS_P1_E2, (void*)web_set_amp_on);
	http_handle_parm(p_content, JS_P1_E3, (void*)web_set_heart_tp);
	http_handle_parm(p_content, JS_P1_E4, (void*)web_set_back_tp);
	http_handle_parm(p_content, JS_P1_E5, (void*)web_set_consle);
	http_handle_parm(p_content, JS_P1_E6, (void*)web_set_fmfre);
	http_handle_parm(p_content, JS_P1_E7, (void*)web_set_fm_tp);

	http_sprintf_init();
	http_sprintf(HTML_CGI_JUMP, LOCAL_IP, C_PAGE_NAME);
	http_sprintf_send();
}







